<template>
  <div class="ai-container">
    <!-- 顶部展示区域 -->
    <el-row :gutter="20" class="ai-header">
      <el-col :span="24">
        <div class="ai-title">
          <h1>智能AI助手</h1>
          <p>基于星火大模型X1的强大能力，为您提供智能问答、内容创作等服务</p>
        </div>
      </el-col>
    </el-row>

    <!-- 功能展示区 -->
    <el-row :gutter="20" class="ai-features">
      <el-col :xs="24" :sm="12" :lg="8">
        <el-card shadow="hover" class="feature-card">
          <div class="feature-icon">
            <el-icon size="40"><ChatDotRound /></el-icon>
          </div>
          <h3>智能问答</h3>
          <p>解答各类知识问题，提供专业准确的回答</p>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="8">
        <el-card shadow="hover" class="feature-card">
          <div class="feature-icon">
            <el-icon size="40"><Document /></el-icon>
          </div>
          <h3>内容创作</h3>
          <p>生成文章、报告、邮件等各种文本内容</p>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="8">
        <el-card shadow="hover" class="feature-card">
          <div class="feature-icon">
            <el-icon size="40"><DataAnalysis /></el-icon>
          </div>
          <h3>数据分析</h3>
          <p>处理和分析数据，提供可视化结果</p>
        </el-card>
      </el-col>
    </el-row>

    <!-- AI交互区域 -->
    <el-row class="ai-interaction">
      <el-col :span="24">
        <el-card class="ai-chat-container">
          <!-- 聊天记录展示区 -->
          <div class="ai-messages" ref="messagesContainer">
            <div v-for="(msg, index) in messages" :key="index" :class="['message', msg.role]">
              <div class="message-content">
                <div class="message-avatar">
                  <el-avatar v-if="msg.role === 'assistant'" :icon="UserFilled" />
                  <el-avatar v-else :icon="Avatar" />
                </div>
                <div class="message-text">
                  <div class="message-role">{{ msg.role === 'assistant' ? 'AI助手' : '我' }}</div>
                  <div class="message-body">{{ msg.content }}</div>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 输入区域 -->
          <div class="ai-input-area">
            <el-input
              v-model="inputMessage"
              type="textarea"
              :rows="3"
              placeholder="请输入您的问题或指令..."
              resize="none"
              @keyup.enter="sendMessage"
            ></el-input>
            <div class="ai-input-actions">
              <el-button type="primary" @click="sendMessage" :loading="loading">
                <el-icon><Promotion /></el-icon> 发送
              </el-button>
              <el-button @click="clearMessages">
                <el-icon><Delete /></el-icon> 清空
              </el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 模型信息 -->
    <el-row class="ai-footer">
      <el-col :span="24">
        <div class="model-info">
          <el-tag type="info">当前模型: 星火大模型X1</el-tag>
          <el-tag type="success">状态: 在线</el-tag>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, nextTick } from 'vue'
import { getAI } from "@/api/AI/AI";

// 消息数据
const messages = ref([
  {
    role: 'assistant',
    content: '您好！我是智能AI助手，请问有什么可以帮您的吗？'
  }
])

const inputMessage = ref('')
const loading = ref(false)
const messagesContainer = ref(null)

// 发送消息
const sendMessage = async () => {
  if (!inputMessage.value.trim()) return;
  
  // 添加用户消息
  messages.value.push({
    role: 'user',
    content: inputMessage.value
  });
  
  const userMessage = inputMessage.value;
  inputMessage.value = '';
  
  // 滚动到底部
  await nextTick();
  scrollToBottom();
  
  // 开始AI回复
  loading.value = true;
  
  try {
    // 添加初始AI消息占位
    let aiMessageIndex = messages.value.push({
      role: 'assistant',
      content: ''
    }) - 1;
    
    // 调用API
    const response = await getAI(userMessage);
    
    // 处理响应数据（假设API返回完整文本）
    if (typeof response === 'string') {
      // 如果是普通文本响应
      messages.value[aiMessageIndex].content = response;
      console.log(response[0]);
      console.log(response);
    } else if (response.choices && response.choices[0].message) {
      // 如果是OpenAI格式的响应
      messages.value[aiMessageIndex].content = response.choices[0].message.content;
    } else if (response.reasoning_content) {
      // 如果是其他格式的响应
      messages.value[aiMessageIndex].content = response.reasoning_content;
    }
    
    // 滚动到底部
    await nextTick();
    scrollToBottom();
    
  } catch (error) {
    console.error('请求出错:', error);
    messages.value.push({
      role: 'assistant',
      content: error.message || '抱歉，处理您的请求时出错了。请稍后再试。'
    });
  } finally {
    loading.value = false;
    await nextTick();
    scrollToBottom();
  }
};

//清空消息
const clearMessages = () => {
  messages.value = [{
    role: 'assistant',
    content: '对话已重置，请问有什么可以帮您的吗？'
  }]
}

// 滚动到底部
const scrollToBottom = () => {
  if (messagesContainer.value) {
    messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
  }
}
</script>

<style scoped lang="scss">
.ai-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  
  .ai-header {
    margin-bottom: 30px;
    text-align: center;
    
    .ai-title {
      h1 {
        font-size: 2.5rem;
        margin-bottom: 10px;
        color: #303133;
      }
      
      p {
        font-size: 1.1rem;
        color: #606266;
      }
    }
  }
  
  .ai-features {
    margin-bottom: 30px;
    
    .feature-card {
      height: 100%;
      text-align: center;
      transition: transform 0.3s;
      
      &:hover {
        transform: translateY(-5px);
      }
      
      .feature-icon {
        margin-bottom: 15px;
        color: #409EFF;
      }
      
      h3 {
        margin-bottom: 10px;
        font-size: 1.3rem;
      }
      
      p {
        color: #909399;
      }
    }
  }
  
  .ai-interaction {
    margin-bottom: 20px;
    
    .ai-chat-container {
      border-radius: 10px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      
      .ai-messages {
        height: 500px;
        overflow-y: auto;
        padding: 15px;
        background-color: #f5f7fa;
        border-radius: 8px;
        margin-bottom: 15px;
        
        .message {
          margin-bottom: 15px;
          
          &.user {
            .message-content {
              flex-direction: row-reverse;
              
              .message-text {
                align-items: flex-end;
                margin-right: 10px;
              }
            }
          }
          
          &.assistant {
            .message-content {
              flex-direction: row;
              
              .message-text {
                align-items: flex-start;
                margin-left: 10px;
              }
            }
          }
          
          .message-content {
            display: flex;
            
            .message-avatar {
              flex-shrink: 0;
            }
            
            .message-text {
              display: flex;
              flex-direction: column;
              max-width: 80%;
              
              .message-role {
                font-size: 0.8rem;
                color: #909399;
                margin-bottom: 5px;
              }
              
              .message-body {
                padding: 10px 15px;
                background-color: white;
                border-radius: 8px;
                box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
                word-break: break-word;
              }
            }
          }
        }
      }
      
      .ai-input-area {
        .ai-input-actions {
          display: flex;
          justify-content: flex-end;
          margin-top: 10px;
          
          .el-button {
            margin-left: 10px;
          }
        }
      }
    }
  }
  
  .ai-footer {
    text-align: center;
    
    .model-info {
      .el-tag {
        margin: 0 5px;
      }
    }
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .ai-container {
    padding: 10px;
    
    .ai-header {
      .ai-title {
        h1 {
          font-size: 1.8rem;
        }
        
        p {
          font-size: 1rem;
        }
      }
    }
    
    .ai-interaction {
      .ai-chat-container {
        .ai-messages {
          height: 300px;
        }
      }
    }
  }
}
</style> 